package com.avic.modules.system.service.impl;

import com.avic.common.service.impl.BaseServiceImpl;
import com.avic.common.utils.QueryHelpPlus;
import com.avic.dozer.service.IGenerator;
import com.avic.modules.log.domain.vo.LogEnumeration;
import com.avic.modules.log.domain.vo.LogVo;
import com.avic.modules.log.service.LogsService;
import com.avic.modules.system.domain.Dept;
import com.avic.modules.system.domain.Job;
import com.avic.modules.system.service.JobService;
import com.avic.modules.system.service.dto.JobDto;
import com.avic.modules.system.service.dto.JobQueryCriteria;
import com.avic.modules.system.service.mapper.DeptMapper;
import com.avic.modules.system.service.mapper.JobMapper;
import com.avic.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class JobServiceImpl extends BaseServiceImpl<JobMapper, Job> implements JobService {

    private final IGenerator generator;

    private final DeptMapper deptMapper;

    private final LogsService logsService;

    @Override
    public Map<String, Object> queryAll(JobQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<Job> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        List<JobDto> convert = generator.convert(page.getList(), JobDto.class);
        for(JobDto jobDto:convert){
            jobDto.setDisabled(jobDto.getEnabled()?false:true);
        }
        map.put("content", convert);
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    public List<Job> queryAll(JobQueryCriteria criteria) {
        List<Job> jobList = baseMapper.selectList(QueryHelpPlus.getPredicate(Job.class, criteria));
        for (Job job : jobList) {
            job.setDept(deptMapper.selectById(job.getDeptId()));
        }
        return jobList;
    }

    @Override
    public List<Job> queryAlls(JobQueryCriteria criteria,Pageable pageable) {
        String order = "sort asc,id desc";
        getPageSize(pageable,order);
        List<Job> list = baseMapper.selectList(QueryHelpPlus.getPredicate(Job.class, criteria));
        for (Job job: list) {
            job.setDept(deptMapper.selectById(job.getDeptId()));
        }
        return list;
    }

    @Override
    public void download(List<JobDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        long sum = 1;
        for (JobDto job : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("序号",sum);
            map.put("名称", job.getName());
            if(job.getDept()!=null){
                Dept dept = deptMapper.selectById(job.getDept().getId());
                map.put("所属部门",dept.getName());
            } else {
                map.put("所属部门","");
            }
            map.put("排序", job.getSort());
            Boolean enabled = job.getEnabled();
            if(enabled){
                map.put("状态", "启用");
            } else {
                map.put("状态", "禁用");
            }
            if(job.getCreateTime()!=null){
                String s = job.getCreateTime().toString();
                String substring = s.substring(0, s.length() - 2);
                map.put("创建日期", substring);
            } else {
                map.put("创建日期", "");
            }
            sum++;
            list.add(map);
        }

        // 创建操作日志
        LogVo logVo = new LogVo();
        String name = "[导出]了"+ "[岗位信息]";
        logVo.setBehavior(name);
        logVo.setAction(LogEnumeration.LEADING_OUT.getMessage());
        logsService.save(logVo);

        FileUtil.downloadExcel(list, response);
    }
}
